﻿@namespace BlazorFluentUI
@inherits ButtonParameters

@{
    var isSplitButton = (Split && OnClick.HasDelegate && MenuItems != null);
    var isCompoundButton = SecondaryText != null;
}
<span class="ms-Button-flexContainer"
      @ref="RootElementReference">
    @if (IconName != null || IconSrc != null)
    {
        <Icon ClassName="ms-Button-icon" IconName=@IconName IconSrc=@IconSrc />
    }
    @if (Text != null || (isCompoundButton && SecondaryText != null))
    {
        <span class="ms-Button-textContainer">
            <span class="ms-Button-label">@Text</span>
            @if (isCompoundButton && SecondaryText != null)
            {
                <span class="ms-Button-description">@SecondaryText</span>
            }
        </span>
    }
    @if (AriaDescripton != null)
    {
        <span class="ms-Button-screenReaderText">@AriaDescripton</span>
    }
    @if (Text == null && ContentTemplate != null)
    {
        @ContentTemplate
    }
    @if (!isSplitButton && MenuItems != null && !HideChevron)
    {
        <Icon IconName="chevron_down" ClassName="ms-Button-menuIcon" />
    }
    @if (MenuItems != null && ContextMenuShown)
    {
        if (ButtonBase == null)
            ButtonBase = this;

        if (MenuItemTemplate != null)
        {

            <ContextualMenu FabricComponentTarget=@ButtonBase
                            ShouldFocusOnMount=@_menuShouldFocusOnMount
                            OnDismiss=@(isDismissed => {

                                   ContextMenuShownChanged.InvokeAsync(false);
                               })
                            Items=@MenuItems
                            DirectionalHint=@DirectionalHint.BottomLeftEdge
                            ItemTemplate=@MenuItemTemplate
                            SubordinateItemTemplate=@SubordinateItemTemplate>

            </ContextualMenu>
        }
        else
        {
            <ContextualMenu FabricComponentTarget=@ButtonBase
                            ShouldFocusOnMount=@_menuShouldFocusOnMount
                            OnDismiss=@(isDismissed => {

                                   ContextMenuShownChanged.InvokeAsync(false);
                               })
                            Items=@MenuItems
                            DirectionalHint=@DirectionalHint.BottomLeftEdge>
            </ContextualMenu>
        }


    }
</span>

@code {
    bool _menuShouldFocusOnMount = true;

    [Parameter] public FluentUIComponentBase? ButtonBase { get; set; }
    [Parameter] public bool ContextMenuShown { get; set; }
    [Parameter] public EventCallback<bool> ContextMenuShownChanged { get; set; }
}
